package com.fhz.day008;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //1.操作class
        //1.1
//        Class clazz  = User.class;
//        String simpleName = clazz.getSimpleName();
//        Log.i(TAG, "onCreate: "+simpleName);

        //1.2
//        User user = new User();
//        Class<? extends User> aClass = user.getClass();

        //1.3
        try {
            Class<?> aClass = Class.forName("com.fhz.day008.User");

            //字段上面的注解
            Field email = aClass.getDeclaredField("email");
            email.setAccessible(true);
            MyAnno annotation = email.getAnnotation(MyAnno.class);
            Log.i(TAG, "onCreate: "+annotation.value());

//            User user = (User) aClass.newInstance();

//            user.name = "123";
//            Log.i(TAG, "onCreate: "+user.name);

            //和字段相关的方法
//            fieldTest(aClass, user);

            //方法的测试
//            extracted(aClass, user);

            //构造方法的测试
//            Constructor<?>[] constructors = aClass.getConstructors();
//            for (Constructor<?> constructor : constructors) {
//                Log.i(TAG, "onCreate: "+constructor.getName());
//            }
//
//            Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
//            for (Constructor<?> declaredConstructor : declaredConstructors) {
//                Log.i(TAG, "onCreate: "+declaredConstructor.getName());
//            }

//            Constructor<?> constructor = aClass.getConstructor(null);
//            User user = (User) constructor.newInstance(null);
//            user.name  = "123";
//            Log.i(TAG, "onCreate: "+user.name);

//            Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
//            declaredConstructor.setAccessible(true);
//            User  user = (User) declaredConstructor.newInstance("哈哈");
//            Log.i(TAG, "onCreate: "+user.name);





        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void extracted(Class<?> aClass, User user) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //方法
        Method[] methods = aClass.getMethods();
        Log.i(TAG, "onCreate: "+methods.length);
        for (Method method : methods) {
            Log.i(TAG, "onCreate: "+method.getName());
        }

        Log.i(TAG, "onCreate: ==========");
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            Log.i(TAG, "onCreate: "+declaredMethod.getName());
        }

        Method setName = aClass.getDeclaredMethod("setName", String.class);
        setName.setAccessible(true);
        setName.invoke(user,"哈哈哈");

        Log.i(TAG, "onCreate: "+ user.name);
    }

    private void fieldTest(Class<?> aClass, User user) throws NoSuchFieldException, IllegalAccessException {

        //获取类的所有公开的字段
//            Field[] fields = aClass.getFields();
//            Log.i(TAG, "onCreate: "+fields.length);

        //获取类的所有声明的字段
//            Field[] declaredFields = aClass.getDeclaredFields();
//            Log.i(TAG, "onCreate: "+declaredFields.length);
        //根据指定的名称取到指定的额字段 user.name="大明"
        Field nameField = aClass.getField("name");
        nameField.set(user,"大明");
        Log.i(TAG, "onCreate: "+ user.name);


        Field address = aClass.getDeclaredField("address");
        address.setAccessible(true);
        address.set(user,"天津");
        Log.i(TAG, "onCreate: "+ user.getAddress());
    }


}